package Jets.ui.skins
{
   import flash.display.BitmapData;
   import flash.display.Graphics;
   import flash.events.Event;
   import flash.geom.Matrix;
   import flash.geom.Point;
   import flash.geom.Rectangle;
   
   import Jets.display.JSprite;
   
   public class Bmp9Skin extends Skin
   {
	  static public var somthing:Boolean = false;
	  
      public function Bmp9Skin(nomal:BitmapData, over:BitmapData=null, down:BitmapData=null, select:BitmapData=null, rt:Rectangle=null,repeat:Boolean=false) 
	  {
         super(nomal,over,down,select,rt,repeat);
         if(rect == null)
         {
            rect = (normal as BitmapData).rect;
         }
      }
      
      override public function draw(event:Event) : void 
	  {
         var bmpd:BitmapData = null;
         var _width:int = 0;
         var _height:int = 0;
         var ui:JSprite = event.target as JSprite;
         switch(ui.state)
         {
            case SkinStates.OVER:
               bmpd = over;
               break;
            case SkinStates.DOWN:
               bmpd = down;
               break;
            case SkinStates.SELECTED:
               bmpd = select;
               break;
            default:
               bmpd = normal;
         }
         
         _width = ui.width;
         _height = ui.height;
         bmpd = (bmpd) || (normal);
         ui.graphics.clear();
         if((isNaN(_width)) || (isNaN(_height)) || _width == 0 || _height == 0 || rect==null)
         {
            return;
         }
		 var right:int = bmpd.width - rect.right;
		 var bottom:int = bmpd.height - rect.bottom;
		 var left:int = rect.x;
		 var top:int = rect.y;
		 var ma:Matrix = new Matrix();
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(0,0,left,top);
		 ma.translate(-rect.right,0);
		 ma.translate(_width - right,0);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(_width - right,0,right,top);
		 ma.identity();
		 ma.translate(-rect.right,-rect.bottom);
		 ma.translate(_width - right,_height - bottom);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(_width - right,_height - bottom,right,bottom);
		 ma.identity();
		 ma.translate(0,-rect.bottom);
		 ma.translate(0,_height - bottom);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(0,_height - bottom,left,bottom);
		 ma.identity();
		 ma.translate(-left,-top);

		 if(repeat)
		 {
			 
		 }
		 else
		 {
			 ma.scale((_width - right - left) / rect.width,(_height - top - bottom) / rect.height);
			 ma.translate(left,top);
			 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 }
		 try
		 {
			 ui.graphics.drawRect(left,top,_width - left - right,_height - top - bottom);
		 }
		 catch(e:Error)
		 {
			 return;
		 }
		 ma.identity();
		 ma.translate(-left,0);
		 ma.scale((_width - right - left) / rect.width,1);
		 ma.translate(left,0);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(left,0,_width - left - right,top);
		 ma.identity();
		 ma.translate(0,-top);
		 ma.scale(1,(_height - top - bottom) / rect.height);
		 ma.translate(0,top);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(0,top,left,_height - top - bottom);
		 ma.identity();
		 ma.translate(-left,-top - rect.height);
		 ma.scale((_width - right - left) / rect.width,1);
		 ma.translate(left,_height - bottom);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(left,_height - bottom,_width - left - right,bottom);
		 ma.identity();
		 ma.translate(-rect.right,-top);
		 ma.scale(1,(_height - top - bottom) / rect.height);
		 ma.translate(_width - right,top);
		 ui.graphics.beginBitmapFill(bmpd,ma,true,somthing);
		 ui.graphics.drawRect(_width - right,top,right,_height - top - bottom);
		 ui.graphics.endFill();
      }
	  
   }
}
